home *** CD-ROM | disk | FTP | other *** search
/ Best Tools for JAVA / Best Tools for JAVA.iso / JAVA_ALL / IDE / SUBARTIC / SUB_ARCT / LIB / ROW.JAV < prev    next >
Encoding:
Text File  |  1996-10-04  |  15.9 KB  |  531 lines

  1.  
  2. package sub_arctic.lib;
  3.  
  4. import sub_arctic.lib.base_parent_interactor;
  5. import sub_arctic.lib.interactor;
  6. import sub_arctic.constraints.std_function;
  7. import sub_arctic.constraints.constraint;
  8. import sub_arctic.output.drawable;
  9. import sub_arctic.output.color_pair;
  10.  
  11. /**
  12.  * This object puts its children in a tiled row, from left to right. They
  13.  * can be aligned along the TOP, CENTER, or BOTTOM. 
  14.  *
  15.  * @author Scott Hudson
  16.  */
  17. public class row extends base_parent_interactor {
  18.  
  19.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  20.  
  21.   /**
  22.    * If this variable is set, the interactor has a box around it.
  23.    */
  24.   protected boolean _boxed=true;
  25.  
  26.   /**
  27.    * Return whether or not the interactor has a box around it.
  28.    * @return boolean true if the row has a box around it
  29.    */
  30.   public boolean boxed() { return _boxed;};
  31.  
  32.   /**
  33.    * Control whether or not the interactor has a box around it. 
  34.    *@param boolean b new state of boxedness
  35.    */
  36.   public void set_boxed(boolean b) { 
  37.     _boxed=b;
  38.     damage_self();
  39.   }
  40.  
  41.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  42.  
  43.   /**
  44.    * This holds our state variable about whether or not we are
  45.    * sized by our children.  This variable can only be set at
  46.    * construction time.
  47.    */
  48.   protected boolean _size_by_children=true;
  49.  
  50.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  51.  
  52.   /**
  53.    * This holds the color pair in use for this object. null indicates
  54.    * to use the system default color pair.
  55.    */
  56.   protected color_pair _colors;
  57.  
  58.   /**
  59.    * Return the current color pair. If you get null, we are using the
  60.    * system's default color pair. Note that the color pair for this
  61.    * interactor is <STRONG>not</STRONG> consulted if the object is
  62.    * not opaque.
  63.    *
  64.    * @return color_pair the current color pair
  65.    */
  66.   public color_pair colors() { 
  67.     if (_colors!=null) {
  68.       return _colors;
  69.     } else {
  70.       return manager.default_color_pair();
  71.     }
  72.   }
  73.  
  74.   /**
  75.    * Set the current color pair. If you set this to null, you'll get
  76.    * the system default color pair. 
  77.    * @param color_pair cp the new color_pair for this interactor
  78.    */
  79.   public void set_colors(color_pair cp) {
  80.     _colors=cp;
  81.     damage_self();
  82.   }
  83.  
  84.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  85.  
  86.   /**
  87.    * If this variable is set, the interactor has an opaque background.
  88.    */
  89.   protected boolean _opaque=true;
  90.  
  91.   /**
  92.    * Return whether or not the interactor has an opaque background
  93.    * @return boolean true if the row is opaque
  94.    */
  95.   public boolean opaque() { return _opaque;};
  96.  
  97.   /**
  98.    * Control whether or not the interactor has an opaque background
  99.    * @param boolean b new state of opaqueness
  100.    */
  101.   public void set_opaque(boolean b) { 
  102.     _opaque=b;
  103.     damage_self();
  104.   }
  105.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  106.  
  107.   /** Border (whitespace) around all the children.  */
  108.   protected int _border;
  109.  
  110.   /** Return the border around all the children (in pixels).  
  111.   * @return int the size of the border in pixels */
  112.   public int border() {return _border;}
  113.  
  114.   /** Set border all around the children. 
  115.   * @param int v the size of the border around the children in pixels
  116.   */
  117.   public void set_border(int v) 
  118.     {
  119.       /* set the value then incorporate that back into constraints */
  120.       _border = v;
  121.       set_child_constraints();
  122.     }
  123.  
  124.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  125.  
  126.   /** The spacing between children.  */
  127.   protected int _interchild_space;  
  128.  
  129.   /** 
  130.    * return the spacing between children.  
  131.    * @return int the space between the children in the row (in pixels)
  132.    */
  133.   public int interchild_space() {return _interchild_space;}
  134.  
  135.   /** Set the spacing between children. 
  136.    *@param int v the new interchild spacing
  137.    */
  138.   public void set_interchild_space(int v) 
  139.     {
  140.       /* set the value then incorporate that back into constraints */
  141.       _interchild_space = v;
  142.       set_child_constraints();
  143.     }
  144.  
  145.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  146.  
  147.   /** Constant to denote centered layout. */
  148.   public static final byte CENTER_JUSTIFIED = 0;
  149.  
  150.   /** Constant to denote left justified layout. */
  151.   public static final byte TOP_JUSTIFIED = 1;
  152.  
  153.   /** Constant to denote right justified layout */
  154.   public static final byte BOTTOM_JUSTIFIED = 2;
  155.  
  156.   /** The layout type for this row. */
  157.   protected byte _layout_type;
  158.  
  159.   /** 
  160.    * The layout type for this row. 
  161.    * @return byte the type of layout in use in this interactor (must be one 
  162.    *              of the constants CENTER_JUSTIFIED, TOP_JUSTIFIED, 
  163.    *              BOTTOM_JUSTIFIED).
  164.    */
  165.   public byte layout_type() {return _layout_type;}
  166.  
  167.   /** 
  168.    * Set the layout type for this row.
  169.    * @param byte v the type of layout you want (must be one of the constants 
  170.    *               CENTER_JUSTIFIED, TOP_JUSTIFIED, BOTTOM_JUSTIFIED).
  171.    */
  172.   public void set_layout_type(byte v) 
  173. {
  174.       /* verify layout type */
  175.       if ((v != CENTER_JUSTIFIED) && (v != TOP_JUSTIFIED) && 
  176.       (v != BOTTOM_JUSTIFIED)) 
  177.     {
  178.           throw new sub_arctic_error(
  179.         "Unrecognized layout type ("+v+") in set_layout_type()");
  180.         }
  181.  
  182.       /* set the value then incorporate that back into constraints */
  183.       _layout_type = v;
  184.       set_child_constraints();
  185.     }
  186.  
  187.    //had:
  188.    //* @exception bad_value this is thrown if you give a bad constant
  189.  
  190.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  191.  
  192.   /** 
  193.    * Full constructor for row.
  194.    * @param int        xv  the x position of this interactor.
  195.    * @param int        yv  the y position of this interactor.
  196.    * @param int        wv  the width of this interactor.
  197.    * @param int        hv  the height of this interactor.
  198.    * @param int        b   the border around the children of this row.
  199.    * @param int        ic  the inter-child spacing for this row.
  200.    * @param boolean    box whether or not you want a box around this interactor.
  201.    * @param boolean    o   whether or not the object has an opaque background.
  202.    * @param boolean    sbc size by children (true if you want this object to 
  203.    *                       derive its size from its children).
  204.    * @param byte       b   the type of layout you want for this row (must be 
  205.    *                       one of the constants CENTER_JUSTIFIED, 
  206.    *                       TOP_JUSTIFIED, BOTTOM_JUSTIFIED).
  207.    * @param color_pair cp  the color pair to use if this object is opaque (if 
  208.    *                       you use null here you'll get the system's default 
  209.    *                       color pair).
  210.    */
  211.   public row(
  212.     int xv, int yv, 
  213.     int wv, int hv, 
  214.     int b, int ic, 
  215.     boolean box, 
  216.     boolean o, 
  217.     boolean sbc, 
  218.     byte lt, 
  219.     color_pair cp) 
  220. {
  221.       super(xv,yv,wv,hv);
  222.  
  223.       /* stash values */
  224.       _border = b;
  225.       _opaque= o;
  226.       _interchild_space = ic;
  227.       _boxed=box;
  228.       _colors=cp;
  229.       _size_by_children=sbc;
  230.       
  231.       /* verify layout type */
  232.       if ((lt != CENTER_JUSTIFIED) && (lt != TOP_JUSTIFIED) && 
  233.       (lt != BOTTOM_JUSTIFIED)) 
  234.     {
  235.           throw new sub_arctic_error("Unrecognized layout type ("+lt+") in row()");
  236.         }
  237.       _layout_type = lt;
  238.     }
  239.  
  240.    //had:
  241.    //* @exception bad_value if unrecognized layout type is requested.
  242.    //* @exception general PROPAGATED.
  243.  
  244.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  245.  
  246.   /** 
  247.    * Smaller row constructor. This assumes that you want to just set the
  248.    * actual parameters of the row and you will use constraints to do all
  249.    * sizing  & positioning.  It uses the system's default colors for 
  250.    * opaque backgrounds.
  251.    * 
  252.    * @param int     b   the border around the children of this row.
  253.    * @param int     ic  the inter-child spacing for this row.
  254.    * @param boolean box whether or not you want a box around this interactor.
  255.    * @param boolean o   whether or not the object is opaque.
  256.    * @param byte    b   the type of layout you want for this row (must be one 
  257.    *                    of the constants CENTER_JUSTIFIED, TOP_JUSTIFIED, 
  258.    *                    BOTTOM_JUSTIFIED).
  259.    */
  260.   public row(int b, int ic, boolean box, boolean o, byte lt) 
  261. {
  262.       super(0,0,10,10);
  263.  
  264.       /* stash values */
  265.       _border = b;
  266.       _opaque= o;
  267.       _boxed=box;
  268.       _interchild_space = ic;
  269.       _colors=null;
  270.  
  271.       /* verify layout type */
  272.       if ((lt != CENTER_JUSTIFIED) && (lt != TOP_JUSTIFIED) && 
  273.       (lt != BOTTOM_JUSTIFIED)) 
  274.     {
  275.           throw new sub_arctic_error("Unrecognized layout type ("+lt+") in row()");
  276.         }
  277.       _layout_type = lt;
  278.     }
  279.  
  280.    //had:
  281.    //* @exception bad_value if unrecognized layout type is requested.
  282.    //* @exception general PROPAGATED.
  283.  
  284.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  285.  
  286.   /** 
  287.    * Minimal row constructor. This assumes that you want to use 
  288.    * constraints for size & position. It makes the row not have a
  289.    * border and be opaque and have no whitespace border around
  290.    * its edges. It uses the system default colors.
  291.    *
  292.    * @param int  ic the inter-child spacing for this row.
  293.    * @param byte b  the type of layout you want for this row (must be one of 
  294.    *                the constants CENTER_JUSTIFIED, TOP_JUSTIFIED, 
  295.    *                BOTTOM_JUSTIFIED).
  296.    */
  297.   public row(int ic, byte lt) 
  298. {
  299.       super(0,0,10,10);
  300.  
  301.       /* stash values */
  302.       _border = 0;
  303.       _opaque= true;
  304.       _boxed= false;
  305.       _interchild_space = ic;
  306.       _colors=null;
  307.  
  308.       /* verify layout type */
  309.       if ((lt != CENTER_JUSTIFIED) && (lt != TOP_JUSTIFIED) && 
  310.       (lt != BOTTOM_JUSTIFIED)) 
  311.     {
  312.           throw new sub_arctic_error("Unrecognized layout type ("+lt+") in row()");
  313.         }
  314.       _layout_type = lt;
  315.     }
  316.  
  317.    //had:
  318.    //* @exception bad_value if unrecognized layout type is requested.
  319.    //* @exception general PROPAGATED.
  320.  
  321.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  322.  
  323.   /** 
  324.    * Set up the constraint for this object's width and height. 
  325.    */
  326.   public void set_local_constraints() 
  327. {
  328.       constraint width_constraint, height_constraint;
  329.  
  330.       /* if we are not sized by our children, then just don't do anything */
  331.        if (!_size_by_children) {
  332.     return;
  333.        }
  334.  
  335.       /* now set up the width and height constraints of this object */
  336.       /* width is sum of width of children +  border */
  337.       /* note: this is NOT 2*border because we took one of them into account */
  338.       /* when laying out the first child's x position */
  339.       width_constraint=std_function.offset(LAST_CHILD.X2(),border());
  340.  
  341.       /* height is max height + 2*border */
  342.       height_constraint=std_function.offset(MAX_CHILD.H(),2*border());
  343.  
  344.       /* snap them on */
  345.       set_w_constraint(width_constraint);
  346.       set_h_constraint(height_constraint);
  347.     }
  348.  
  349.    //had:
  350.    //* @exception general PROPAGATED
  351.  
  352.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  353.  
  354.   /** Establish or reestablish constraints on children to form a row. 
  355.    */
  356.   protected void set_child_constraints() 
  357. {
  358.       int i, max = num_children();
  359.       interactor current_child;
  360.       constraint strut_between_children, yposition = null;
  361.  
  362.       /* loop over all children */
  363.       for (i=0; i<max; ++i) 
  364.     {
  365.           /* set the current child */
  366.           current_child = child(i);
  367.  
  368.           /* constraint syntax is: object code, part code, fun code, parm */
  369.  
  370.           /* set up the strut between children */
  371.           if (i != 0) 
  372.         {
  373.           /* this is the strut which keeps the y position of a child 
  374.            * below the previous child */
  375.           strut_between_children = std_function.offset(PREV_SIBLING.X2(),
  376.                                        interchild_space());
  377.           current_child.set_x_constraint(strut_between_children);
  378.             } 
  379.       else 
  380.         {
  381.           /* i is zero, setup the top constraint */
  382.           strut_between_children = std_function.offset(PARENT.X(), border());
  383.           current_child.set_x_constraint(strut_between_children);
  384.             }
  385.  
  386.           /* build y constraint based on layout type */
  387.           if (layout_type() == CENTER_JUSTIFIED) 
  388.         yposition=std_function.centered(PARENT.H(), 0);
  389.           else if (layout_type() == TOP_JUSTIFIED) 
  390.         yposition = std_function.offset(PARENT.Y(), border());
  391.           else if (layout_type() == BOTTOM_JUSTIFIED) 
  392.         yposition = std_function.far_edge_just(PARENT.Y2(), border());
  393.  
  394.       /* set the constraint */
  395.           current_child.set_y_constraint(yposition);
  396.         }
  397.  
  398.       /* set this object's own constraints */
  399.       set_local_constraints();
  400.   }
  401.  
  402.    //had:
  403.    //* @exception general PROPAGATED
  404.  
  405.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  406.  
  407.   /** 
  408.    * Draw the object on the provided drawable
  409.    * @param drawable d the surface to draw on 
  410.    */ 
  411.   protected void draw_self_local(drawable d) 
  412. {
  413.       /* clear the background */
  414.       if (opaque()) {
  415.     d.setColor(colors().background());
  416.     d.fillRect(0,0,w()-1,h()-1);
  417.       }
  418.       /* draw a bounding box */
  419.       if (boxed()) {
  420.     d.drawRect(0, 0, w()-1, h()-1); 
  421.       }
  422.       draw_children(d);
  423.     }
  424.  
  425.    //had:
  426.    //* @exception general PROPAGATED 
  427.  
  428.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  429.  
  430.   /** 
  431.    * Overridden from base_interactor to also add constraints 
  432.    * @param int        at_indx the index position at which to change the child
  433.    * @param interactor chld    the new child interactor for that position
  434.    */
  435.   public void set_child(int at_indx, interactor chld) 
  436. {
  437.       super.set_child(at_indx,chld);
  438.       set_child_constraints();
  439.     }
  440.  
  441.    //had:
  442.    //* @exception op_not_supported PROPAGATED
  443.    //* @exception general PROPAGATED 
  444.  
  445.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  446.  
  447.   /** Override from base_interactor to also add constraints 
  448.   * @param interactor chld the child interactor to add 
  449.   */
  450.   public void add_child(interactor chld) 
  451. {
  452.       super.add_child(chld);
  453.       set_child_constraints();
  454.     }
  455.  
  456.   //had:
  457.   //* @exception op_not_supported PROPAGATED
  458.   //* @exception general PROPAGATED 
  459.  
  460.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  461.  
  462.   /** 
  463.    * Override to also add constraints 
  464.    * @param int        at_indx  the index position at which to insert the child
  465.    * @param interactor chld the child interactor to insert
  466.    */
  467.   public void insert_child(int at_indx, interactor chld) 
  468. {
  469.       super.insert_child(at_indx,chld);
  470.       set_child_constraints();
  471.     }
  472.  
  473.    //had:
  474.    //* @exception op_not_supported PROPAGATED
  475.    //* @exception general PROPAGATED 
  476.  
  477.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  478.  
  479.   /** 
  480.    * Override to also add constraints
  481.    * @param int at_indx the index position of the child to remove
  482.    */
  483.   public interactor remove_child(int at_indx) 
  484. {
  485.       interactor i;
  486.  
  487.       i=super.remove_child(at_indx);
  488.       set_child_constraints();
  489.       return i;
  490.     }
  491.  
  492.    //had:
  493.    //* @exception op_not_supported PROPAGATED
  494.    //* @exception general PROPAGATED 
  495.  
  496.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  497.  
  498.   /** 
  499.    * Override to also add constraints 
  500.    * @param interactor the_child the child to remove
  501.    */
  502.   public void remove_child(interactor the_child) 
  503. {
  504.       super.remove_child(the_child);
  505.       set_child_constraints();
  506.     }
  507.  
  508.    //had:
  509.    //* @exception op_not_supported PROPAGATED
  510.    //* @exception general PROPAGATED 
  511.  
  512.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  513. }
  514.  
  515. /*=========================== COPYRIGHT NOTICE ===========================
  516.  
  517. This file is part of the subArctic user interface toolkit.
  518.  
  519. Copyright (c) 1996 Scott Hudson and Ian Smith
  520. All rights reserved.
  521.  
  522. The subArctic system is freely available for most uses under the terms
  523. and conditions described in 
  524.   http://www.cc.gatech.edu/gvu/ui/sub_arctic/sub_arctic/doc/usage.html 
  525. and appearing in full in the lib/interactor.java source file.
  526.  
  527. The current release and additional information about this software can be 
  528. found starting at: http://www.cc.gatech.edu/gvu/ui/sub_arctic/
  529.  
  530. ========================================================================*/
  531.